import { BaseModel } from "./BaseModel"
import * as THREE from 'three'
import { EventBus } from '../utils/EventBus'
// 游船物体类
export class Ship extends BaseModel {
  init() {
    this.isMouseTouching = false

    this.scene.add(this.model)

    this.isCameraMove = false
    // 当前位置的索引
    this.ponitIndex = 0
    // 设置游船物体的属性
    this.model.position.set(150, 0, -80)
    this.model.rotation.set(0, -Math.PI / 2, 0)
    this.model.scale.set(100, 100, 100) // 游船物体很小要与大城市模型匹配需要放大

    this.generatorMovePath()
    this.onModelAttach()
  }
  //设置运动坐标
  generatorMovePath() {
    const shipPath = new THREE.CatmullRomCurve3([
      new THREE.Vector3(134.356097129589, 2.0112688541412354, -78.91746888546072),
      new THREE.Vector3(13.132075955743915, 2.0112688541412425, -69.85260460470285),
      new THREE.Vector3(13.132075955743915, 2.0112688541412425, -69.85260460470285),
      new THREE.Vector3(-80.28995611104816, 2.0112688541412282, -12.640254617216172),
      new THREE.Vector3(-71.5470123066941, 2.0112688541412354, 25.641138454485144),
      new THREE.Vector3(-71.5470123066941, 2.0112688541412354, 25.641138454485144),
      new THREE.Vector3(-17.5179164111899, 2.0112688541412354, 139.95062075065943),
      new THREE.Vector3(-67.10547001341894, 2.0112688541412354, 64.30494908329582),
      new THREE.Vector3(-87.03568940230136, 2.0112688541412354, 20.40776369519459),
      new THREE.Vector3(-88.0509634357777, 2.0112688541412425, -32.429601593890354),
      new THREE.Vector3(-70.27457116256328, 2.0112688541412425, -50.370253013515836),
      new THREE.Vector3(-39.206573479212764, 2.0112688541412425, -64.28841112963838),
      new THREE.Vector3(47.33347662423566, 2.0112688541412354, -73.13885409538068),
      new THREE.Vector3(134.356097129589, 2.0112688541412354, -78.91746888546072),
    ])
    this.pointsArr = shipPath.getSpacedPoints(10000)
    // const geometry = new THREE.BufferGeometry().setFromPoints(this.pointsArr)
    // const material = new THREE.LineBasicMaterial({ color: 0x00ff00, side: THREE.DoubleSide })
    // const line = new THREE.Line(geometry, material)
    // this.scene.add(line)
  }
  onTick(t) {
    if (this.ponitIndex < this.pointsArr.length - 1) {
      const { x, y, z } = this.pointsArr[this.ponitIndex + 1]
      if (this.isCameraMove) {
        // 没有被按下 
        if (!this.isMouseTouching) {
          this.camera.lookAt(x, y + 20, z)
        }
        this.camera.position.set(x, y + 20, z)

      }
      this.model.position.copy(this.pointsArr[this.ponitIndex])
      this.model.lookAt(this.pointsArr[this.ponitIndex + 1])
      this.ponitIndex += 1
    } else {
      this.ponitIndex = 0
    }

  }

  // 鼠标按下
  mousedownFn = () => {
    this.isMouseTouching = true // 鼠标已经按下
  }
  // 鼠标移动
  mousemoveFn = (e) => {
    if (this.isMouseTouching) { // 只有按下时进入此逻辑代码
      // 旋转核心思想：在原有的旋转角度基础上，新增移动的偏移量，乘以 0.01 让旋转弧度降低
      // rotateY() 在上一次旋转的角度上继续新增你传入的弧度数值
      // rotation.y = 直接赋予一个旋转的最终弧度数值
      this.camera.rotateY((this.prePos - e.clientX) * 0.01)
    }

    this.prePos = e.clientX
  }
  // 鼠标抬起
  mouseupFn = () => {
    this.isMouseTouching = false
    this.prePos = undefined // 清空上一次记录的坐标点位置
  }

  onModelAttach() {
    EventBus.getInstance().on('mode-roaming', isOpen => {
      if (isOpen) {
        window.addEventListener('mousedown', this.mousedownFn)
        window.addEventListener('mousemove', this.mousemoveFn)
        window.addEventListener('mouseup', this.mouseupFn)
      } else {
        window.removeEventListener('mousedown', this.mousedownFn)
        window.removeEventListener('mousemove', this.mousemoveFn)
        window.removeEventListener('mouseup', this.mouseupFn)
      }
    })
  }
}